import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import EmployerJobListView from '../views/EmployerJobListView.vue'
import JobEditView from '../views/JobEditView.vue'
import UserProfileView from '../views/UserProfileView.vue'
import JobListView from '../views/JobListView.vue'
import CandidateVitaeEditView from '../views/CandidateVitaeEditView.vue'
import JobCreateView from '../views/JobCreateView.vue'
import JobDetailView from '../views/JobDetailView.vue'
import CandidateListView from '../views/CandidateListView.vue'
import CandidateVitaeView from '../views/CandidateVitaeView.vue'
import ChatWindow from '../views/ChatWindow.vue'
import InformView from '../views/InformView.vue'
import CommunityView from '../views/CommunityView.vue'
import CommunityDetailView from '../views/CommunityDetailView.vue'
import PublishPostView from '../views/PublishPostView.vue'

const routes = [
  {
    path: '/',
    name: 'Login',
    component: LoginView,
    meta: {
      hideNavbar: true, // 标记不需要显示导航栏
      requiresAuth: false // 标记不需要认证
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: RegisterView,
    meta: {
      hideNavbar: true, // 标记不需要显示导航栏
      requiresAuth: false // 标记不需要认证
    }
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/employerJobList',
    name: 'employerJobList',
    component: EmployerJobListView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/job/edit/:positionId',
    name: 'JobEdit',
    component: JobEditView,
    props: true,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/user/profile',
    name: 'UserProfile',
    component: UserProfileView,
    meta: {
      hideNavbar: true, // 标记不需要显示导j航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/jobList',
    name: 'JobList',
    component: JobListView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/vitae/edit',
    name: 'CandidateVitaeEdit',
    component: CandidateVitaeEditView,
    meta: {
      hideNavbar: true, // 标记不需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/job/create',
    name: 'JobCreate',
    component: JobCreateView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/job/detail/:positionId',
    name: 'JobDetail',
    component: JobDetailView,
    props: true,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/job/candidateList/:positionId',
    name: 'CandidateList',
    component: CandidateListView,
    props: true,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/candidate/vitae',
    name: 'CandidateVitae',
    component: CandidateVitaeView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  // 在 router/index.js 中修改 /chat 路由配置
  {
    path: '/chat',
    name: 'Chat',
    component: ChatWindow,
    props: true,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    },
    beforeEnter: (to, from, next) => {
      // 确保每次导航都会触发组件更新
      next()
    }
  },
  {
    path: '/inform',
    name: 'Inform',
    component: InformView,
    meta: {
      hideNavbar: true, // 标记不需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/community',
    name: 'Community',
    component: CommunityView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/community/post/:postId',
    name: 'PostDetail',
    component: CommunityDetailView,
    props: true,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  },
  {
    path: '/community/publish',
    name: 'PublishPost',
    component: PublishPostView,
    meta: {
      hideNavbar: false, // 标记需要显示导航栏
      requiresAuth: true // 标记需要认证
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.meta.requiresAuth) {
    // 检查用户是否已登录
    const token = localStorage.getItem('token')
    if (token) {
      // 用户已登录，允许访问
      next()
    } else {
      // 用户未登录，重定向到登录页面
      next('/')
    }
  } else {
    // 不需要认证的路由，直接访问
    next()
  }
})

export default router
